package net.wanho.productManage.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.productManage.dao.ProductDAO;
import net.wanho.productManage.po.Product;

import java.util.List;

public class ProductDAOImpl extends BaseDAOImpl<Product> implements ProductDAO {
    @Override
    public List<Product> selectAll() throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  PRODUCT_ID  productId,\n" +
                    "  PROD_TYPE \"productType.typeId\",\n" +
                    "  PROD_NAME prodName,\n" +
                    "  PROD_STANDARD prodStandard,\n" +
                    "  COST_PRICE costPrice,\n" +
                    "  RETAIL_PRICE retailPrice,\n" +
                    "  REMARKS remarks,\n" +
                    "  STATUS \"status\",\n" +
                    "  CREATE_TIME createTime,\n" +
                    "  UPDATE_TIME updateTime\n" +
                    "FROM product where status = '1' ";
            Object[] params = {};
            return super.queryForList(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public Product findById(String id) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  PRODUCT_ID  productId,\n" +
                    "  PROD_TYPE \"productType.typeId\",\n" +
                    "  PROD_NAME prodName,\n" +
                    "  PROD_STANDARD prodStandard,\n" +
                    "  COST_PRICE costPrice,\n" +
                    "  RETAIL_PRICE retailPrice,\n" +
                    "  REMARKS remarks,\n" +
                    "  STATUS \"status\",\n" +
                    "  CREATE_TIME createTime,\n" +
                    "  UPDATE_TIME updateTime\n" +
                    "FROM product WHERE PRODUCT_ID=?";
            Object[] params = {id};
            return super.queryForObject(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
